package cn.kone.system.controller;


import cn.kone.base.utlis.R;
import cn.kone.system.annotation.PreAuthorize;
import cn.kone.system.domain.Menu;
import cn.kone.system.query.MenuQuery;
import cn.kone.system.service.IMenuService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
@Api(tags = "菜单接口类")
public class MenuController {
    @Autowired
    private IMenuService menuService;

    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询全部菜单")
    @PreAuthorize(name = "查询全部菜单", sn = "menu:findAllMenus",descs = "查询全部菜单")
    public R<List<Menu>> findAllMenus() {

        List<Menu> all = menuService.findAll();
        return R.ok(all);
    }

    /**
     * 查询单个
     *
     * @param id 用户id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个菜单")
    @PreAuthorize(name = "查询单个菜单", sn = "menu:get",descs = "查询单个菜单")
    public R<Menu> findOneMenus(@PathVariable("id") Long id) {
        Menu one = menuService.findOne(id);
        return R.ok(one);
    }

    @PutMapping
    @ApiOperation(value = "新增和修改菜单")
    @PreAuthorize(name = "新增和修改菜单", sn = "menu:addAndUpdateMenu",descs = "新增和修改菜单")
    public R<Void> addAndUpdateMenu(@RequestBody Menu menu) {
        Long id = menu.getId();
        try {
            if (id == null) {
                menuService.addMenu(menu);
            } else {
                menuService.updateMenu(menu);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    /**
     * 删除单个
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单个菜单")
    @PreAuthorize(name = "删除单个菜单", sn = "menu:deleteMenus",descs = "删除单个菜单")
    public R<Void> deleteMenus(@PathVariable("id") Long id) {
        try {
            menuService.deleteMenu(id);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    /**
     * 分页查询
     *
     * @param menuQuery
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询菜单")
    @PreAuthorize(name = "分页查询菜单", sn = "menu:pageQuery",descs = "分页查询菜单")
    public PageInfo<Menu> pageQuery(@RequestBody MenuQuery menuQuery) {

        List<Menu> list = menuService.pageQuery(menuQuery);
        return new PageInfo<Menu>(list);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除菜单")
    @PreAuthorize(name = "批量删除菜单", sn = "menu:batchDeleteMenu",descs = "批量删除菜单")
    public R<Void> batchDeleteMenu(@RequestBody Long[] ids) {
        try {
            menuService.batchDeleteMenu(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }

    /**
     * 查询类型树
     * @param id 前端传过来的文章类型ID
     * @return
     */
    @GetMapping("/menuTree/{id}")
    @ApiOperation(value = "查询父类型")
    public List<Menu> menuTree(@PathVariable("id") Long id){
        //查询所有类型
        List<Menu> list = menuService.findAll();

        return menuService.menuTree(id,list);
    }


}
